package sms_interface;
/**
 * 
 * @author aayushjain
 * To verify whether the message sent by the user is of the right format or not.
 */
public class MessageVerification {
	
	/**
	 * classroom stores the room on which operations have to be done.
	 */
	static String classroom;
	/**
	 * appliance stores the appliance no. on which we will be doing operations.
	 */
	static String appliance;
	/**
	 * Verifying whether the message is of the correct format or not. Parses the message and checks various fields.
	 * the following three are the correct formats
	 * EN:-Get state <appliance no.> <room no.>ID:<unique id>.
	 * EN:-Get data <appliance no.> <room no.> <timestamp(time units)>.ID:<unique id>.
	 * EN:-Set state ON/OFF <appliance no.> <room no.>.ID:<unique id>.
	 * @param message Takes the message received from the GSM gateway as the input.
	 * @return true if the message is of the correct format. false if not.
	 */
	public static boolean verify(String message)
	{
		String arr2[] =message.split("%");
		/*for ( int i=0;i<arr2.length;i++){
			System.out.println(arr2[i]);
		}*/
		
		String first=arr2[2].substring(2,arr2[2].length());
		//System.out.println(first);
		if(first.equalsIgnoreCase("Get"))
		{
			int flag=0;
			String second=arr2[3].substring(2,arr2[3].length());
			//System.out.println(second);
			if( second.equalsIgnoreCase("state"))
			{
				flag=1;
			}
			else if( second.equalsIgnoreCase("data"))
			{
				int flag3=0;
				flag=2;
				String four=arr2[6].substring(2,arr2[6].length());
				try{
					int num=Integer.parseInt(four);
					if(num<0 || num>60){
						return false;
					}
				}catch(Exception exp){
					return false;
				}
				String five=arr2[7].substring(2,arr2[7].length());
				if(five.equalsIgnoreCase("min")||five.equalsIgnoreCase("sec"))
				{
					
					flag3=1;
				}
				if(flag3==0)
				{
					return false;
				}
				if( flag3==1)
				{
					String third=arr2[8].substring(2,arr2[8].length());
					if(!third.equalsIgnoreCase("id"))
					{
						return false;
					}
				}	
				classroom=arr2[5].substring(2,arr2[5].length());
				appliance=arr2[4].substring(2,arr2[4].length());
				
			}
			else
			{
				return false;
			}
			if( flag==1)
			{
				String third=arr2[6].substring(2,arr2[6].length());
				if(!third.equalsIgnoreCase("id"))
				{
					return false;
				}
				classroom =arr2[5].substring(2,arr2[5].length());
				appliance=arr2[4].substring(2,arr2[4].length());
				
			}
			
		}
		else if(first.equalsIgnoreCase("Set"))
		{
			String second=arr2[3].substring(2,arr2[3].length());
			//System.out.println(second);
			if(!second.equalsIgnoreCase("state"))
			{
				return false;
			}
			int flag2=0;
			String third=arr2[4].substring(2,arr2[4].length());
			if( third.equalsIgnoreCase("on")|| third.equalsIgnoreCase("off"))
			{
				flag2=1;
			}
			else
			{
				return false;
			}
			if(flag2==1)
			{
				String fourth=arr2[7].substring(2,arr2[7].length());
				if(!fourth.equalsIgnoreCase("id"))
				{
					return false;
				}	
			}
			classroom=arr2[6].substring(2,arr2[6].length());
			appliance=arr2[5].substring(2,arr2[5].length());
			
		}
		else
		{
			return false;
		}
		String last=arr2[arr2.length-2].substring(2, arr2[arr2.length-2].length());
		try{
			int id_check=Integer.parseInt(last);
		}catch(Exception exp2){
			return false;
		}
		
		
		return true;
	}
	
	/**
	 * Returns classroom no.
	 * @return classroom no. from the query
	 */
	public String getclass(){
		return classroom;
	}
	
	/**
	 * Returns appliance no. 
	 * @return appliance no. from the query.
	 */
	public String getAppliance(){
		return appliance;
	}
	
	
	/*public static void main(String args[]){
		//System.out.println(verify("EN%3A%2DGet%20state%20f1%20l1%21Id%3A100%21"));
		//System.out.println(verify("EN%3A%2DDo%20state%20f1%20l1%21Id%3A100%21"));
		//System.out.println(verify("EN%3A%2DGet%20stats%20f1%20l1%21Id%3A100%21"));
		//System.out.println(verify("EN%3A%2DGet%20state%20f1%20l1%21I%3A100%21"));
		//System.out.println(verify("EN%3A%2DSet%20state%20ON%20f1%20l1%21Id%3A100%21"));
		//System.out.println(verify("EN%3A%2DGet%20data%20f1%20l1%205%20min%21Id%3A100%21"));
		//System.out.println(verify("EN%3A%2DGet%20data%20f1%20l1%2010%20min%21Id%3A100%21"));
		//System.out.println(verify("EN%3A%2DGet%20data%20f1%20l1%20t%20min%21Id%3A100%21"));
		/*System.out.println(verify("EN%3A%2DGet%20data%20f1%20l1%207%20sec%21Id%3A100%21"));
		System.out.println(verify("EN%3A%2DGet%20data%20f1%20l1%209%20min%21Id%3Aabc%21"));
		System.out.println(verify("EN%3A%2DGet%20state%20f1%20l1%21Id%3A100%21"));
		System.out.println(verify("EN%3A%2DGet%20state%20f1%20l1%21Id%3Aabc%21"));
		
		System.out.println(verify("EN%3A%2DSet%20state%20on%20f1%20l1%21Id%3A100%21"));
		System.out.println(verify("EN%3A%2DSet%20state%20off%20f1%20l1%21Id%3Aabc%21"));
		*/
	//}
}
